//
//  DHOrderManagerViewController.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/1/11.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHOrderManagerViewController.h"
#import "DHOrder_LeasedCell.h"
#import "DHOrder_ReturnCell.h"
#import "DHReturnViewController.h"
#import "DHOrderProductView.h"
#import "DHOrderReturnView.h"

@interface DHOrderManagerViewController ()<UIScrollViewDelegate>


@property (weak, nonatomic) IBOutlet UILabel *backNumberLabel;
@property (weak, nonatomic) IBOutlet UIView *lineView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *lineViewLeftLayout;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) DHOrderReturnView *returnView;
@property (strong, nonatomic) DHOrderProductView *productView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSafeAreaLayout;

@property (strong, nonatomic) UIButton *returnButton; // 归还按钮
@property (strong, nonatomic) NSMutableArray *returnProductArray;
@property (strong, nonatomic) NSArray *returnArray;

@end

@implementation DHOrderManagerViewController

- (NSArray *)returnArray{
    if (!_returnArray) {
        _returnArray = [NSArray array];
    }
    return _returnArray;
}

- (NSMutableArray *)returnProductArray{
    if (!_returnProductArray) {
        _returnProductArray = [NSMutableArray array];
    }
    return _returnProductArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"订单管理";
    if ([UIDevice systemVersion] < 11) {
        self.topSafeAreaLayout.constant = 64;
    }
    
    [self initReturnButton];
    
    [self loadReturnProduct];
    
    self.productView = [[DHOrderProductView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.width, self.scrollView.height)];
    self.returnView = [[DHOrderReturnView alloc] initWithFrame:CGRectMake(self.productView.right, 0, self.scrollView.width, self.scrollView.height)];
    [self.scrollView addSubview:self.productView];
    [self.scrollView addSubview:self.returnView];
    
    self.scrollView.contentSize = CGSizeMake(kScreenWidth * 2, self.scrollView.height);
    
    addNObserver(@selector(loadReturnProduct), @"loadReturnData");
    addNObserver(@selector(loadReturnProduct), @"compensate_pay_success");
}

- (void)dealloc{
    removeNObserver(@"loadReturnData");
}

- (void)backAction{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.productView.frame = CGRectMake(0, 0, self.scrollView.width, self.scrollView.height);
    self.returnView.frame = CGRectMake(self.productView.right, 0, self.scrollView.width, self.scrollView.height);
}

- (void)initReturnButton{
    self.returnButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [self.returnButton setTitle:@"归还" forState:UIControlStateNormal];
    self.returnButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.returnButton setTitleColor:[UIColor colorWithHexString:@"#4a4a4a"] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.returnButton];
    
    WeakSelf(weakSelf)
    [self.returnButton bk_addEventHandler:^(id sender) {
        if (weakSelf.returnProductArray.count > 0) {
            DHReturnViewController *retrunController = [[DHReturnViewController alloc] initWithProductArray:weakSelf.returnProductArray];
            [weakSelf.navigationController pushViewController:retrunController animated:YES];
        }else{
            [NSObject showHudTipStr:@"请选择归还衣物"];
        }
    } forControlEvents:UIControlEventTouchUpInside];
    
    self.returnButton.hidden = YES;
}

// 订单按钮事件
- (IBAction)orderAction:(id)sender {
    self.returnButton.hidden = YES;
    [UIView animateWithDuration:0.3 animations:^{
        self.lineViewLeftLayout.constant = 0;
    }];
    [self.scrollView setContentOffset:CGPointMake(0, 0)];
}

// 归还按钮事件
- (IBAction)backAction:(id)sender {
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.width, 0)];
    self.returnButton.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.lineViewLeftLayout.constant = 67;
    }];
}

// 获取待归还服饰
- (void)loadReturnProduct{
    [[DHNetAPIClient shareDHNetAPIClient] request_products_returnAndBlock:^(id data, NSError *error) {
        self.returnArray = [data objectForKey:@"products"];
        if (self.returnArray.count == 0) {
            self.backNumberLabel.hidden = YES;
        }else{
            self.backNumberLabel.hidden = NO;
            self.backNumberLabel.text = [NSString stringWithFormat:@"%lu", (unsigned long)self.returnArray.count];
        }
        [self.returnView reloadDataWithReturnArray:self.returnArray];
    }];
}

- (void)routerEventWithName:(NSString *)eventName userInfo:(NSDictionary *)userInfo{
    if ([eventName isEqualToString:@"DHOrder_ReturnCell"]) {
        if ([[userInfo objectForKey:@"state"] isEqualToString:@"add"]) {
            [self.returnProductArray addObject:[userInfo objectForKey:@"productDic"]];
        }else{
            [self.returnProductArray removeObject:[userInfo objectForKey:@"productDic"]];
        }
        self.returnView.selectedReturnArr = self.returnProductArray;
    }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x / self.scrollView.width == 0) {
        [self orderAction:nil];
    }else if (scrollView.contentOffset.x / self.scrollView.width == 1){
        [self backAction:nil];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
